import random

import logging
from django import http
from django.views import View
from django_redis import get_redis_connection


from celery_tasks.sms import constants
from celery_tasks.sms.tasks import send_sms_code
from meiduo_mall.utils.response_code import RETCODE
from verifications.libs.captcha.captcha import captcha
logger = logging.getLogger()

class ImageCodeView(View):
    # 图形验证码

    def get(self,request,uuid):
        """
        :param request: 请求对象
        :param uuid: 唯一标识图形验证码所属于的用户
        :return: image/jpg
        """
        # 生成图形验证码
        text,image=captcha.generate_captcha()
        # 保存图形验证码
        redis_conn = get_redis_connection('verify_code')

        redis_conn.setex('img_%s'% uuid,300,text)
        # 响应图形验证码
        return http.HttpResponse(image,content_type='image/jpg')


class SmsCodeView(View):
    def get(self,request,mobile):
        """
        :param reqeust: 请求对象
        :param mobile: 手机号
        :return: JSON
        """
        # 接收参数
        image_code_client = request.GET.get('image_code')
        uuid = request.GET.get('uuid')
        # 校验参数
        if not all([image_code_client,uuid]):
            return http.JsonResponse({'code':RETCODE.NECESSARYPARAMERR,'errmsg':'缺少必传参数'})
        # 判断用户是否频繁发送验证码
        # 创建连接到redis的对象
        redis_conn = get_redis_connection('verify_code')
        # 提取验证码的标记
        send_flag = redis_conn.get('send_flag_%s'% mobile)
        if send_flag:
            return http.JsonResponse({'code':RETCODE.THROTTLINGERR,'errmsg':'短信发送过于频繁'})

        # 提取图形验证码
        image_code_sever = redis_conn.get('img_%s'% uuid)
        if image_code_client  is None:
            return http.JsonResponse({'code':RETCODE.IMAGECODEERR,'errmsg':'图形验证码已经失效'})
        # 删除图形验证码，避免恶意测试图形验证码
        redis_conn.delete('img_%s' % uuid)
        # 对比图形验证码
        image_code_sever = image_code_sever.decode()  # bytes转字符串
        if image_code_sever.lower() != image_code_client.lower():  #转为小写之后比较
            return http.JsonResponse({'code':RETCODE.IMAGECODEERR,'errmsg':'图形验证码有误'})
        # 生成短信验证码：生成6位数验证码
        sms_code = '%06d' % random.randint(0,999999)
        logger.info(sms_code)
        # # 保存短信验证码
        # redis_conn.setex('sms_%s' % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code)
        # redis_conn.setex('send_flag_%s'% mobile,constants.SEND_SMS_CODE_INTERVAL,1)
        #创建rides管道
        pl = redis_conn.pipeline()
        #添加命令导管道中
        pl.setex('sms_%s' % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code)
        pl.setex('send_flag_%s' % mobile, constants.SEND_SMS_CODE_INTERVAL, 1)
        #执行命令
        pl.execute()

        # 发送短信验证码
        # CCP().send_template_sms(mobile,[sms_code, constants.SMS_CODE_REDIS_EXPIRES // 60],constants.SEND_SMS_TEMPLATE_ID)
        # 使用celery
        send_sms_code.delay(mobile, sms_code)
        # 响应结果
        return http.JsonResponse({'code':RETCODE.OK,'errmsg':'验证码发送成功'})

